package com.caffeinatedbliss.nanowrimo.spike;

import com.caffeinatedbliss.nanowrimo.DynamicImageGenerator;
import com.caffeinatedbliss.nanowrimo.NanowrimoComputer;

import java.awt.*;
import java.awt.image.BufferedImage;

public class MiniGraph_v1 implements DynamicImageGenerator {
  public static final int WIDTH = 140;
  public static final int HEIGHT = 55;
  private BufferedImage graphImage;

  public MiniGraph_v1(String historyParameter, int goal) {

    NanowrimoComputer computer = new NanowrimoComputer(historyParameter, goal);
    int current = computer.getCurrent();
    int[] wordCounts = computer.getExpectedWordCount();
    int currentDayOfNaNoWriMo = computer.getCurrentDay();
    int wordCountGoal = wordCounts[currentDayOfNaNoWriMo - 1];

    int daysUsed = (int) (2 + (WIDTH - 10) * (double) currentDayOfNaNoWriMo / 30.0);
    int currentPos = (int) (4 + (WIDTH - 10) * (double) current / (double) goal);
    int expectedPos = (int) (4 + (WIDTH - 10) * (double) wordCountGoal / (double) goal);

    graphImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics g = graphImage.getGraphics();

    // Draw background
    g.setColor(new Color(0xee, 0xee, 0xee));
    g.fillRect(1, 1, WIDTH - 2, HEIGHT - 2);
    g.setColor(Color.black);
    g.drawRect(0, 0, WIDTH, HEIGHT);

    Font textFont = new Font("dialog", Font.BOLD, 11);
    g.setFont(textFont);
    FontMetrics fm = g.getFontMetrics(textFont);
    int ascent = fm.getAscent();
    int descent = fm.getDescent();
    int totalLineHeght = ascent + descent;
    String wordCount = current + " words";
    int wordCountWidth = fm.stringWidth(wordCount);
    int leftEdgeWordCount = 70 - wordCountWidth / 2;
    g.drawString(wordCount, leftEdgeWordCount, totalLineHeght);

    String dayCount = "Goal: " + wordCountGoal + " words";
    wordCountWidth = fm.stringWidth(dayCount);
    leftEdgeWordCount = 70 - wordCountWidth / 2;
    g.drawString(dayCount, leftEdgeWordCount, totalLineHeght * 2 + 18);

    // Draw graph box
    g.drawRect(3, totalLineHeght + 6, WIDTH - 8, 12);

    // Draw tasteful shadows and boundries
    g.setColor(Color.GRAY);
    g.drawLine(5, totalLineHeght + 19, WIDTH - 4, totalLineHeght + 19);
    g.drawLine(WIDTH - 4, totalLineHeght + 8, WIDTH - 4, totalLineHeght + 19);
    g.setColor(Color.LIGHT_GRAY);
    g.drawLine(7, totalLineHeght + 20, WIDTH - 3, totalLineHeght + 20);
    g.drawLine(137, totalLineHeght + 10, WIDTH - 3, totalLineHeght + 20);

    // Draw overall % bar
    g.setColor(Color.GRAY);
    g.fillRect(4, totalLineHeght + 7, daysUsed, 11);
    g.setColor(Color.WHITE);
    g.fillRect(4 + daysUsed, totalLineHeght + 7, WIDTH - 9 - daysUsed, 11);
    g.setColor(Color.BLACK);
    g.drawLine(3 + daysUsed, totalLineHeght + 7, 3 + daysUsed, totalLineHeght + 17);

    int[] wordX = {currentPos - 4, currentPos, currentPos + 5};
    int[] wordY = {totalLineHeght + 1, totalLineHeght + 6, totalLineHeght + 1};
    g.setColor(new Color(0x00, 0x80, 0x00));
    g.fillPolygon(wordX, wordY, 3);

    int[] ewordX = {expectedPos - 5, expectedPos, expectedPos + 4};
    int[] ewordY = {totalLineHeght + 23, totalLineHeght + 18, totalLineHeght + 23};
    g.setColor(new Color(0xAA, 0x00, 0x00));
    g.fillPolygon(ewordX, ewordY, 3);
    g.dispose();
  }

  public BufferedImage getGraphImage() {
    return graphImage;
  }
}
